
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadStatus: false,
    id: '',
    type: '',
    detail: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // options.id = 3;
    console.log('联盟详情', options);
    this.setData({
      id: options.id,
      type: options.type ? options.type : ''
    })

    wx.showLoading();
    this.get_detail(() => {
      wx.hideLoading();
      this.setData({
        loadStatus: true
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '你的好友邀请你加入'+this.data.detail.name,
      path: '/pages/alliance/detail/index?id='+this.data.id+'&type=join'
    }
  },
  //获取详情
  get_detail(callback) {
    let that = this;
    app.request({
      url: 'alliance/detail',
      data: {
        id: that.data.id
      },
      sCallback:(res) => {
        if(res.code == 1) {
          that.setData({
            detail: res.data
          })

          callback && callback();
        } else {
          app.alert(res.msg, () => {
            wx.navigateBack();
          })
        }
      }
    })
  },
  //编辑成员
  edit(e) {
    let that = this;
    let index = app.getBindParam(e, 'index');
    let status = app.getBindParam(e, 'status');
    let item = this.data.detail.member[index];
    let tips = '确认通过该申请吗？';
    if(status == 2) {
      tips = '确认删除该成员吗？';
    }

    wx.showModal({
      title: '提示',
      content: tips,
      success: function(e) {
        console.log(e)
        if(e.confirm) {
          app.request({
            url: 'alliance/editMember',
            data: {
              id: item.id,
              status: status
            },
            sCallback: (res) => {
              if(res.code == 1) {
                app.alert(res.msg, () => {
                  that.get_detail();
                })
              } else {
                app.alert(res.msg);
              }
            }
          })
        }
      }
    })

  },
  //加入联盟
  join() {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确认加入该联盟吗？',
      success: function(e) {
        if(e.confirm) {
          app.request({
            url: 'alliance/apply',
            data: {
              id: that.data.id
            },
            sCallback: (res) => {
              if(res.code == 1) {
                app.alert(res.msg, () => {
                  wx.redirectTo({
                    url: '/pages/alliance/index/index'
                  })
                })
              } else {
                app.alert(res.msg);
              }
            }
          })
        }
      }
    })
  },
  //机构详情
  school_detail(e) {
    let schoolId = app.getBindParam(e, 'id');
    wx.navigateTo({
      url: '/pages/instdetail/index?schoolId='+schoolId
    })
  }
})